package com.tianci.appstore;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.os.SystemProperties;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;

import java.util.List;


public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LinearLayout root = new LinearLayout(this);

        Button btn = new Button(this);
        btn.setText("SystemProperties.set()");
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SystemProperties.set("my_config", "123123");
            }
        });
        root.addView(btn);

        btn = new Button(this);
        btn.setText("SystemProperties.get()");
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String value = SystemProperties.get("my_config");
                Toast.makeText(MainActivity.this, "get:" + value, Toast.LENGTH_SHORT).show();
            }
        });
        root.addView(btn);
        setContentView(root);


        ActivityManager _ActivityManager = (ActivityManager) this
                .getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningAppProcessInfo> list = _ActivityManager
                .getRunningAppProcesses();
        for (ActivityManager.RunningAppProcessInfo info : list) {
            String pkg = "NULL";
            if (info.pkgList != null) {
                pkg = "";
                for (String s : info.pkgList)
                    pkg += s + ";";
            }
            Log.i("tag", info.processName + ":" + pkg);
        }
    }

    private String getCurrentServicePackage(Context context, String action) {
        String ret = null;
        PackageManager pm = context.getPackageManager();
        List<ResolveInfo> list = pm.queryIntentServices(new Intent(action), 0);
        for (ResolveInfo info : list) {
            if (ret == null && info.serviceInfo.isEnabled() && info.serviceInfo.packageName.equals(context.getPackageName()))
                ret = info.serviceInfo.packageName;
            if (info.serviceInfo.isEnabled() && !info.serviceInfo.packageName.equals(context.getPackageName()))
                ret = info.serviceInfo.packageName;
        }

        return ret;
    }
}
